package com.mq.consumer;

import com.mq.pojo.RespondMessage;
import com.mq.pojo.User;
import com.mq.service.*;
import com.mq.service.impl.WechatPay;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.mq.utils.HashUtils.getMd5String;

/**
 * 用户相关消息的消费者
 */
@Component
public class UserConsumer {
    @Autowired
    private UserService userService;
    @Autowired
    private TravelRecommendationsService travelRecommendationsService;
    @Autowired
    private FlightService flightService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private WechatPay wechatPay;

    /**
     * 处理用户注册消息
     */
    @RabbitListener(queues = "userRegisterQueue")
    public void handleRegister(Map<String, Object> message) {
        try {
            String email = (String) message.get("email");
            String username = (String) message.get("username");
            String password = (String) message.get("password");
            String phone = (String) message.get("phone");

            User user = userService.getUserByUserEmail(email);
            if (user == null) {
                userService.register(email, username, password, phone);
                System.out.println("用户注册成功：" + email);
            } else {
                System.out.println("用户已存在：" + email);
            }
        } catch (Exception e) {
            System.err.println("处理注册消息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 处理用户登录消息
     */
    @RabbitListener(queues = "userLoginQueue")
    public void handleLogin(Map<String, Object> message) {
        try {
            String email = (String) message.get("email");
            String password = (String) message.get("password");

            User loginUser = userService.getUserByUserEmail(email);
            if (loginUser == null) {
                System.out.println("用户不存在：" + email);
                return;
            }

            String inputPasswordHash = getMd5String(password);
            String storedPassword = loginUser.getPassword();

            if (inputPasswordHash.equals(storedPassword)) {
                System.out.println("用户登录成功：" + email);
            } else {
                System.out.println("密码错误：" + email);
            }
        } catch (Exception e) {
            System.err.println("处理登录消息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 处理获取旅行推荐列表消息
     */
    @RabbitListener(queues = "travelRecommendationsQueue")
    public void handleGetTravelRecommendations(Map<String, Object> message) {
        try {
            String userEmail = (String) message.get("userEmail");
            Integer page = (Integer) message.get("page");
            Integer count = (Integer) message.get("count");

            User user = userService.getUserByUserEmail(userEmail);
            travelRecommendationsService.getTravelRecommendationsList(page, count);
            System.out.println("获取旅行推荐列表成功：" + userEmail);
        } catch (Exception e) {
            System.err.println("处理获取旅行推荐列表消息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 处理获取航班列表消息
     */
    @RabbitListener(queues = "flightsListQueue")
    public void handleGetFlightsList(Map<String, Object> message) {
        try {
            String destination = (String) message.get("destination");
            flightService.getFlightsList(destination);
            System.out.println("获取航班列表成功，目的地：" + destination);
        } catch (Exception e) {
            System.err.println("处理获取航班列表消息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 处理锁定订单消息
     */
    @RabbitListener(queues = "lockOrderQueue")
    public void handleLockOrder(Map<String, Object> message) {
        try {
            String userEmail = (String) message.get("userEmail");
            Integer flightId = (Integer) message.get("flightId");

            User user = userService.getUserByUserEmail(userEmail);
            orderService.LockOrder(user, flightId);
            System.out.println("锁定订单成功：用户=" + userEmail + ", 航班ID=" + flightId);
        } catch (Exception e) {
            System.err.println("处理锁定订单消息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 处理取消订单消息
     */
    @RabbitListener(queues = "cancelOrderQueue")
    public void handleCancelOrder(Map<String, Object> message) {
        try {
            String userEmail = (String) message.get("userEmail");
            Integer flightId = (Integer) message.get("flightId");

            User user = userService.getUserByUserEmail(userEmail);
            orderService.CancelLockOrder(user, flightId);
            System.out.println("取消订单成功：用户=" + userEmail + ", 航班ID=" + flightId);
        } catch (Exception e) {
            System.err.println("处理取消订单消息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 处理支付订单消息
     */
    @RabbitListener(queues = "payOrderQueue")
    public void handlePayOrder(Map<String, Object> message) {
        try {
            String userEmail = (String) message.get("userEmail");
            Integer orderId = (Integer) message.get("orderId");

            User user = userService.getUserByUserEmail(userEmail);
            orderService.PayOrder(user, orderId, wechatPay);
            System.out.println("支付订单成功：用户=" + userEmail + ", 订单ID=" + orderId);
        } catch (Exception e) {
            System.err.println("处理支付订单消息时发生错误：" + e.getMessage());
        }
    }

    /**
     * 处理获取订单列表消息
     */
    @RabbitListener(queues = "orderListQueue")
    public void handleGetOrderList(Map<String, Object> message) {
        try {
            String userEmail = (String) message.get("userEmail");
            User user = userService.getUserByUserEmail(userEmail);
            orderService.getOrderList(user);
            System.out.println("获取订单列表成功：" + userEmail);
        } catch (Exception e) {
            System.err.println("处理获取订单列表消息时发生错误：" + e.getMessage());
        }
    }
} 